@extends('admin.home.main')

@section('body')
<section id="main-content">
  <section class="wrapper">
	<h3>教具添加</h3>
	<!-- BASIC FORM ELELEMNTS -->
	<div class="row mt">
	  <div class="col-lg-12">
		<div class="form-panel">
		  <form class="form-horizontal style-form" action="/admin/teaching/aidupdate" method="post" enctype="multipart/form-data" id="art_form">
		  	<input type="hidden" id="_token" name="_token" value="{{csrf_token()}}" readonly="readonly">
        <input type="hidden" name="id" id="id" value="{{$aidInfo->id}}">
        <input type="hidden" name="tid" id="tid" value="{{$aidInfo->at_id}}">
      			<div class="form-group">
      			  <label class="col-sm-2 col-sm-2 control-label">教具名称</label>
      			  <div class="col-sm-10">
      				<input type="text" style="width:400px;" class="form-control" id="name" name="name" placeholder="请输入教具名称" size="50" value="{{$aidInfo->name}}">
      			  </div>
      			</div>

            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">教具介绍</label>
              <div class="col-sm-10">
                <textarea rows="5" cols="30" style="width:400px;" class="form-control" placeholder="请输入教具名称" id="desc" name="desc">{{$aidInfo->desc}}</textarea>
              </div>
            </div>

            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">商品售价</label>
              <div class="col-sm-10">
                <input style="width:400px;" class="form-control" placeholder="请输入商品售价" id="price" name="price" value="{{$aidInfo->price}}">
              </div>
            </div>

            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">市场价</label>
              <div class="col-sm-10">
                <input style="width:400px;" class="form-control" placeholder="请输入市场价" id="mprice" name="mprice" value="{{$aidInfo->mprice}}">
              </div>
            </div>

            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">商品库存</label>
              <div class="col-sm-10">
                <input style="width:400px;" class="form-control" placeholder="请输入商品库存量" id="stock" name="stock" value="{{$aidInfo->stock}}">
              </div>
            </div>

            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">计量单位</label>
              <div class="col-sm-10">
                <input style="width:400px;" class="form-control" placeholder="请输入商品库存量" id="unit" name="unit" value="{{$aidInfo->unit}}">
              </div>
            </div>

            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">教具详情</label>
              <div class="col-sm-10">
                <a class="btn btn-theme02" href="javascript:;" onclick="uploadPhoto()">选择图片</a>
                <input type="file" id="material_file" style="display:none;" name="material_file" onchange="upload()">
                <div class="fileupload-new thumbnail" style="width: 200px; margin-top:10px;">
                    <img id="preview_photo" src="{{$aidInfo->picture}}" alt="">
                </div>
                <input type="hidden" id="imgsrc" name="imgsrc" value="{{$aidInfo->picture}}">
              </div>
            </div>
			
			<button type="button" id="save" class="btn btn-success">提交</button>
			<button type="reset" class="btn btn-primary">重置</button>
		  </form>
		</div>
	  </div>
	  <!-- col-lg-12-->
	</div>

	
  </section>
  <!-- /wrapper -->
</section>
<script type="text/javascript">
    function uploadPhoto() {
        $("#material_file").click();
    }

    function upload(){
      if($('#material_file').val() == ''){
        alert('请选择图片');
        return false;
      }

      var formData = new FormData($('#art_form')[0]);
      
      $.ajax({
        url:'/admin/teaching/upload_teach_aid',
        type:'post',
        data:formData,
        dataType:'json',
        contentType: false,
            processData: false,
            success:function(data){
              if(data.code == 200){
                $('#preview_photo').attr('src','https://boom-dog.oss-cn-zhangjiakou.aliyuncs.com/'+data.imgurl);
                $('#imgsrc').val('https://boom-dog.oss-cn-zhangjiakou.aliyuncs.com/'+data.imgurl);
                alert('上传成功');
              }else{
                alert('上传失败');
              }
              return false;
            }
      })
    }

    $('#save').click(function(){
    	var _token = $('#_token').val(),
          id = $('#id').val(),
          tid = $('#tid').val(),
          name = $('#name').val(),
          desc = $('#desc').val(),
          price = $('#price').val(),
          mprice = $('#mprice').val(),
          stock = $('#stock').val(),
          imgsrc = $('#imgsrc').val(),
          unit = $('#unit').val();

      if(!name){
        alert('请输入教具名称');
        return false;
      }

      if(!desc){
        alert('请输入教具简介');
        return false;
      }

      if(!price){
        alert('请输入教具价格');
        return false;
      }

      if(!stock){
        alert('请输入教具库存量');
        return false;
      }
          

    	$.ajax({
    		url:'/admin/teaching/aidupdate',
    		type:'post',
    		data:{'_token':_token,'id':id,'tid':tid,'name':name,'desc':desc,'price':price,'mprice':mprice,'stock':stock,'picture':imgsrc,'unit':unit},
    		dataType:'json',
    		success:function(data){
    			if(data.code == 200){
    				alert('教具编辑成功');
    				location.href="/admin/teaching/taview/"+tid;
    			}else{
    				alert('教具编辑失败');
    				location.reload();
    			}
    		}
    	})
    })
</script>
@endsection